/**
 * @author  zhanshi Liu
 * @date    2020/04/23
 * @website www.jhmsona.cn
 */
var vm = new Vue({
    el:'#vm',
    data:{
        tableData:[],
        deptTree:[],
        filterText: '',
        currentPage:1,
        count:0,
        dialogFormVisible: false,
        formLabelWidth: '80px',
        files:'',
        fileList:[],//要上传的文件列表
        uploadFlag:false,
        tipFlag:true,
        percent:0,
        fd:new FormData(),
        form:{
            deptId:undefined,
            sysUser:{
                username:undefined,
            },
            originalFileName:undefined,
            page:1,
            limit:10,
        },
    },
    created:function (){
        this.getFileList();
        this.getDeptTree();
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods:{
        getFileList(){
            axios.get('/oss/file/list', {
                params: {
                    data:JSON.stringify(this.form)
                }
            }).then(res => {
                this.tableData=res.data.data;
                this.count = res.data.count;
            })
        },
        getDeptTree(){
            axios.get('/sys/dept/selectList')
                .then(res => {
                    this.deptTree=res.data.data;
                })
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        nodeClick(obj){
            this.form.deptId = obj.id;
            this.getFileList();
        },
        open(){//打开上传窗口
            this.dialogFormVisible = true;
        },
        search(){
            this.getFileList();
        },
        reset(form1){
            this.$refs[form1].resetFields()
        },
        handleSizeChange(val) {
            this.form.limit = val;
            this.getFileList();
        },
        handleCurrentChange(val) {
            this.form.page = val;
            this.getFileList();
        },
        submitUpload(file) {
            this.$refs.upload.submit();
            if(this.fileList.length>0){
                this.fileList.forEach(item => {
                    this.fd.append("files", item.raw);
                });
            }else{
                this.$notify({ title: '提示',message: '请选择要上传的文件',type: 'warning' });
                return false;
            }
            axios({
                url: '/oss/file/add',
                method: 'post',
                headers: {'Content-Type': 'multipart/form-data'},
                onUploadProgress: (progressEvent) => {
                    this.tipFlag=false;//隐藏提示
                    this.uploadFlag=true;//显示上传进度
                    this.percent = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(2))
                },
                data:this.fd,
            }).then(res => {
                this.percent = 0;
                this.uploadFlag=false;
                this.tipFlag=true;
                this.fileList=[];
                this.$refs.upload.clearFiles();
                if (res.status==200){
                    this.$notify({ title: '提示',message: res.data.msg,type: 'success' });
                    this.dialogFormVisible = false;
                    this.getFileList();
                }else{
                    this.$notify({ title: '提示',message: "上传失败",type: 'warning' });
                }

            }).catch(()=> {
                this.percent = 0;
                this.uploadFlag=false;
                this.tipFlag=true;
                this.fileList=[];
                this.$refs.upload.clearFiles();
                this.$notify.error({
                    title: '错误',
                    message: '上传失败'
                });
            });
        },
        uploadFile(file){},
        beforeUpload(file) {
            const flag = file.size / 1024 / 1024 < 50;
            if (!flag) {
                this.$notify({ title: '提示',message: '上传文件大小超出限制!',type: 'warning' });
                this.$refs.upload.abort();
                return flag;
            }
        },
        handleRemove(file, fileList) {
            this.fileList = fileList;
        },
        changeFile(file, fileList){
            this.fileList = fileList;
        },
        remove(row){
            axios.delete('/oss/file/del',{
                params:{ id:row.id}
            }).then(res=> {
                console.log(res)
                if (res.data.status==200){
                    this.$notify({ title: '成功',message: res.data.data.msg,type: 'success' });
                } else {
                    this.$notify({ title: '警告',message: "删除失败",type: 'warning' });
                }
            })
        },
        download(index,row){
            axios.get('/oss/file/download',{
                params:{ id:encodeURI(row.id)},
                responseType:'blob'
            }).then(res=> {
                this.downloadFile(res,row.originalFileName);
            })
        },
        downloadFile(res,fileName){
            let blob = new Blob([res.data],{type: 'application/vnd.ms-excel;charset=utf-8'});
            if ('download' in document.createElement('a')) { // 非IE下载
                const elink = document.createElement('a')
                elink.download = fileName;
                elink.style.display = 'none';
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
            } else { // IE下载
                navigator.msSaveBlob(blob, fileName);
            }
            if (res.status===200){
                this.$notify({ title: '成功',message: "下载成功",type: 'success' });
            }else{
                this.$notify({ title: '警告',message: "下载失败",type: 'warning' });
            }
        }
    }
})

